package org.reyton.dao.impl;

import org.hibernate.SessionFactory;
import org.reyton.dao.SysEmployeeDao;
import org.reyton.entity.SysEmployee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 员工表Dao接口的实现类
 * Created by reyton on 16-11-29.
 */
@Repository("sysEmployeeDao")
public class SysEmployeeDaoImpl extends HibernateDaoSupport implements SysEmployeeDao {

    /***
     * 按名称自动装配
     * @param sessionFactory
     */
    @Autowired
    @Qualifier("sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<SysEmployee> find() {
        return (List<SysEmployee>) this.getHibernateTemplate().find("from SysEmployee");
    }

    @Override
    public SysEmployee findByInfo(String sn, String password) {
        if (this.getHibernateTemplate().find("from SysEmployee where sn = ? and password = ?", sn, password).size() != 1) {
            return null;
        } else {
            return (SysEmployee) this.getHibernateTemplate().find("from SysEmployee where sn = ? and password = ?", sn, password).get(0);
        }
    }

}
